package com.wyh.controller.feemanagementcontroller;

import com.wyh.common.result.Result;
import com.wyh.domain.dto.feemanagementdto.*;
import com.wyh.domain.dto.gfive.LoginUser;
//import com.wyh.domain.po.feemanagementpo.MessageVo;
import com.wyh.domain.po.feemanagementpo.Billes;
import com.wyh.domain.po.gfive.User;
import com.wyh.domain.vo.feemanagementvo.BillssVO;
import com.wyh.domain.vo.feemanagementvo.BillsVO;
import com.wyh.security.gfive.service.TokenService;
import com.wyh.service.feemanagementservice.BillService;
import com.wyh.service.feemanagementservice.CollectionService;
import com.wyh.service.feemanagementservice.ConfigurationService;
import com.wyh.service.feemanagementservice.MailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 账单Controller
 */
@RestController
@RequestMapping("/bill")
@Api(tags = "催收管理")
public class BillController {
    @Autowired
    private BillService billService;
    @Autowired
    private ConfigurationService configurationService;
    @Autowired
    private CollectionService collectionService;
    @Autowired
    private MailService mailService;
    @Autowired
    private TokenService tokenService;

    @PostMapping("/getAllBills")
    @ApiOperation("查询欠费账单")
    public Result<BillsVO> getAllBills(@RequestBody BilDTO bilDTO, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        User user = loginUser.getUser();
        String account = user.getAccount();
        return Result.success(billService.selectAllBills(account,bilDTO));
    }
    @PostMapping("/getAllBill")
    @ApiOperation("查询结清账单")
    public Result<BillssVO> getAllBill(@RequestBody BilDTO bilDTO, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        User user = loginUser.getUser();
        String account = user.getAccount();
        return Result.success(billService.selectAllBill(account,bilDTO));
    }
    @PostMapping("/bills")
    @ApiOperation("账单详情信息")
    public Result<BDTO> selectBillsById(@RequestBody BillStatusDTO billStatusDTO) {
        List<BDTO> bdtos = billService.selectBasic(billStatusDTO);
        List<BiDetailsDTO> biDetailsDTOS = billService.selectBill(billStatusDTO);
        List<BillDetailsDTO> billDetailsDTOS = billService.selectBillDetails(billStatusDTO);

        BDTO bdto = new BDTO();
        BiDetailsDTO biDetailsDTO = new BiDetailsDTO();

        if (!bdtos.isEmpty() || !biDetailsDTOS.isEmpty() || !billDetailsDTOS.isEmpty()) {
            String customername = bdtos.get(0).getCustomername();
            String propertyname = bdtos.get(0).getPropertyname();
            String payee = bdtos.get(0).getPayee();
            bdto.setCustomername(customername);
            bdto.setPropertyname(propertyname);
            bdto.setPayee(payee);
        }else{
            return Result.error("","该用户没有订单");
        }

        BigDecimal accountsreceivable = biDetailsDTOS.get(0).getReceivable();
        String refunddeduction = biDetailsDTOS.get(0).getRefunddeduction();
        String predepositoffset = biDetailsDTOS.get(0).getPredepositoffset();
        BigDecimal arrears = biDetailsDTOS.get(0).getArrears();
        Integer arrearsstatus = biDetailsDTOS.get(0).getArrearsstatus();
        BigDecimal laggold = biDetailsDTOS.get(0).getLaggold();
        String accounttime = biDetailsDTOS.get(0).getAccounttime();
        biDetailsDTO.setReceivable(accountsreceivable);
        biDetailsDTO.setRefunddeduction(refunddeduction);
        biDetailsDTO.setPredepositoffset(predepositoffset);
        biDetailsDTO.setArrears(arrears);
        biDetailsDTO.setArrearsstatus(arrearsstatus);
        biDetailsDTO.setLaggold(laggold);
        biDetailsDTO.setAccounttime(accounttime);

        biDetailsDTO.setDetails(billDetailsDTOS);
        List<BiDetailsDTO> biDetailsDTOSS = new ArrayList<>();
        biDetailsDTOSS.add(biDetailsDTO);

        bdto.setTotal(biDetailsDTOSS);
        return Result.success(bdto);
    }

    @PostMapping("/updateBill")
    @ApiOperation("催收通知")
    public Result<String> collectBill(@RequestBody List<Integer> customerid) {
        //查询催收的方式
        Integer method = configurationService.selectCollectionNotificationMethod();
        //邮件通知
        if (method == 1) {
            String[] email = collectionService.getEmail(customerid);
            // TODO 查询账单详情

            // TODO 生成缴费单

            //发送邮件
            mailService.sendSimple(email);
            //修改催收状态
            for (Integer id : customerid) {
                billService.updateBill(id);
            }
            return Result.success();
        } else if (method == 2) {  //短信通知
            String[] telephone = collectionService.getTelephone(customerid);
            // TODO 查询账单详情

            // TODO 生成缴费单

            //发送短信
            mailService.sendSimple(telephone);
            //修改催收状态
            for (Integer id : customerid) {
                billService.updateBill(id);
            }
            return Result.success();
        } else {  //邮件短信都通知
            String[] email = collectionService.getEmail(customerid);
            String[] telephone = collectionService.getTelephone(customerid);
            // TODO 查询账单详情

            // TODO 生成缴费单

            //发送催收消息
             mailService.sendSimple(email);
             mailService.sendSimple(telephone);
            //修改催收状态
            for (Integer integer : customerid) {
                billService.updateBill(integer);
            }
        }
        return Result.success("催收成功");
    }

    @PostMapping("/updateLaggold")
    @ApiOperation("更新滞纳金/总欠费金额")
    public Result<String> updateLaggold() {
        billService.updateLaggold();
        billService.updateTotalArrears();
        return Result.success("更新滞纳金成功");
    }

}
